package com.antis.olsl.library.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.antis.olsl.library.R;
import com.antis.olsl.library.adapter.ChooseSalesroomAdapter;
import com.antis.olsl.library.response.SalesroomOrWarehouseInfo;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * @ProjectName: ATS_OLSL
 * @Package: com.antis.olsl.dialog
 * @ClassName: ChooseFiledTypeDialog
 * @Description: 选择仓库
 * @Author: 羊琪
 * @CreateDate: 2020/3/24 16:25
 * @UpdateDate: 2020/3/24 16:25
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class ChooseSalesroomDialog {
    private Context context;
    private ArrayList<SalesroomOrWarehouseInfo> salesroomInfos = new ArrayList<>();
    private ArrayList<SalesroomOrWarehouseInfo> chooseSalesroomInfos = new ArrayList<>();
    private ArrayList<SalesroomOrWarehouseInfo> allInfos = new ArrayList<>();
    private ChooseSalesroomListener listener;
    private Dialog dialog;
    private ImageView imgChoose;
    private EditText etSearch;

    private boolean chooseAll;
    private ChooseSalesroomAdapter chooseSalesroomAdapter;
    //用来存储过滤数据与原数据对应下标
    private HashMap<Integer, Integer> map = new HashMap<>();
    private int selectedIndex = -1;

    public ChooseSalesroomDialog(Context context, ArrayList<SalesroomOrWarehouseInfo> salesroomInfos, ChooseSalesroomListener listener) {
        if (salesroomInfos == null) {
            return;
        }
        this.context = context;
        this.salesroomInfos.addAll(salesroomInfos);
        this.allInfos.addAll(salesroomInfos);
        this.listener = listener;

        initView();
    }

    private void initView() {
        dialog = new Dialog(context, R.style.dialog);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_choose_salesroom_or_warehouse, null);
        TextView tvTitle = view.findViewById(R.id.tv_title);
        TextView tvChooseAllTip = view.findViewById(R.id.tv_chooseAllTip);
        imgChoose = view.findViewById(R.id.img_choose);
        etSearch = view.findViewById(R.id.et_search);

        RecyclerView recyclerView = view.findViewById(R.id.recyclerView);
        chooseSalesroomAdapter = new ChooseSalesroomAdapter(salesroomInfos);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setAdapter(chooseSalesroomAdapter);

        chooseSalesroomAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (selectedIndex == position) {
                    return;
                }
                if (selectedIndex != -1) {
                    salesroomInfos.get(selectedIndex).setChoose(false);
                }
                salesroomInfos.get(position).setChoose(!salesroomInfos.get(position).isChoose());
                if (!TextUtils.isEmpty(etSearch.getText().toString()) && map.containsKey(position)) {
                    allInfos.get(map.get(position)).setChoose(salesroomInfos.get(position).isChoose());
                } else {
                    allInfos.get(position).setChoose(salesroomInfos.get(position).isChoose());
                }
                selectedIndex = position;
                changeDataStatus();
            }
        });

        tvTitle.setText("选择门店");
        tvChooseAllTip.setText("全部门店");

        etSearch.setHint("输入门店名称搜索");

        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                filterData(etSearch.getText().toString());
            }
        });


        dialog.setContentView(view);
        Window win = dialog.getWindow();
        if (null != win) {
            win.getDecorView().setPadding(0, 0, 0, 0);
            WindowManager.LayoutParams lp = win.getAttributes();
            lp.width = WindowManager.LayoutParams.FILL_PARENT;
            win.setAttributes(lp);
        }
        RelativeLayout rlChooseAll = view.findViewById(R.id.rl_chooseAll);
        rlChooseAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setAllChooseData();
            }
        });

        view.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        view.findViewById(R.id.ll_top).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });

        TextView tvSure = view.findViewById(R.id.tv_sure);
        tvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    listener.onChooseSalesroomListener(chooseAll, chooseSalesroomInfos, allInfos, selectedIndex);
                }
                dialog.dismiss();
            }
        });

        dialog.show();
        changeDataStatus();
    }

    /**
     * 修改被选中状态
     */
    private void changeDataStatus() {
        chooseSalesroomInfos.clear();
        chooseAll = true;
        String chooseId = "";
        for (int i = 0; i < salesroomInfos.size(); i++) {
            if (salesroomInfos.get(i).isChoose()) {
                selectedIndex = i;
                chooseSalesroomInfos.add(salesroomInfos.get(i));
                chooseAll = false;
                chooseId = salesroomInfos.get(i).getId();
            }
        }

        for (int j = 0; j < allInfos.size(); j++) {
            if (!TextUtils.isEmpty(chooseId) && chooseId.equals(allInfos.get(j).getId())) {
                allInfos.get(j).setChoose(true);
            } else {
                allInfos.get(j).setChoose(false);
            }
        }

        imgChoose.setSelected(chooseAll);
        chooseSalesroomAdapter.setList(salesroomInfos);
    }

    /**
     * 根据关键字过滤显示数据
     *
     * @param key
     */
    private void filterData(String key) {
        if (TextUtils.isEmpty(key)) {
            map.clear();
            salesroomInfos.clear();
            salesroomInfos.addAll(allInfos);
        } else {
            salesroomInfos.clear();
            selectedIndex = -1;
            int j = 0;
            for (int i = 0; i < allInfos.size(); i++) {
                if (allInfos.get(i).getName().contains(key)) {
                    if (allInfos.get(i).isChoose()) {
                        selectedIndex = j;
                    }
                    map.put(j++, i);

                    salesroomInfos.add(allInfos.get(i));
                }
            }
        }
        chooseSalesroomAdapter.setList(salesroomInfos);
    }

    /**
     * 全选/全不选
     */
    private void setAllChooseData() {
        if (chooseAll) {
            return;
        }
        chooseAll = true;
        chooseSalesroomInfos.clear();
        for (int i = 0; i < salesroomInfos.size(); i++) {
            salesroomInfos.get(i).setChoose(false);
            if (!TextUtils.isEmpty(etSearch.getText().toString()) && map.containsKey(i)) {
                allInfos.get(map.get(i)).setChoose(false);
            }
        }
        selectedIndex = -1;
        imgChoose.setSelected(chooseAll);
        chooseSalesroomAdapter.setList(salesroomInfos);
    }

    public void dismiss() {
        if (null != dialog) {
            dialog.dismiss();
        }
    }

    public interface ChooseSalesroomListener {
        /**
         * @param choosedFiledInfos 已过滤的被选择的数组
         * @param sourceInofs       //包含选择与未选择的原数组
         */
        void onChooseSalesroomListener(boolean isChooseAll, ArrayList<SalesroomOrWarehouseInfo> choosedFiledInfos, ArrayList<SalesroomOrWarehouseInfo> sourceInofs, int selectedIndex);
    }

}
